package demo;

import java.util.ArrayList;
import java.util.List;

public class BinrayTree {
    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public  TreeNode(char val){
            this.val = val;
        }
    }

    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;

    }
    //前序遍历
    public void preOrder(TreeNode root){
        if(root == null){
            return ;
        }
        System.out.print(root.val + "   ");
        preOrder(root.left);
        preOrder(root.right);

    }
        List<Character> ret = new ArrayList<>();
    public List<Character> preorderTraversal(TreeNode root) {
        if(root == null){
            return null;
        }
        System.out.print(root.val + "   ");
        ret.add(root.val);
        preOrder(root.left);
        preOrder(root.right);
        return ret;

    }
    //中序遍历
    public void inOrder(TreeNode root){
        if(root == null){
            return ;
        }
        preOrder(root.left);
        System.out.print(root.val + "   ");
        preOrder(root.right);

    }

    public List<Integer> inOrder1(TreeNode root){
        if(root == null){
            return null;
        }
         List<Integer>  list1 = inOrder1(root.left);
        List<Integer> ret = new ArrayList<>();
        ret.addAll(list1);
        System.out.print(root.val + "   ");
        List<Integer> list2 = inOrder1(root.right);
        ret.addAll(list2);
        return ret;

    }
    //后序遍历
    public void postOrder(TreeNode root){
        if(root == null){
            return ;
        }
        preOrder(root.left);
        preOrder(root.right);
        System.out.print(root.val + "   ");

    }
    //节点个数
    public int usedSize;
    public int size1(TreeNode root) {
        if (root == null) {
            return -1;
        }
        usedSize++;
        size1(root.left);
        size1(root.right);
        return usedSize;
    }
    public int size2(TreeNode root){
        if(root == null){
            return 0;
        }
        int ret = size2(root.left) + size2(root.right) + 2;
        return ret;
    }
    // 获取叶子节点的个数
    public int count;

    public int getLeafNodeCount(TreeNode root){
    if(root == null){
        return 0;
    }
    if(root.left == null && root.right == null){
        count++;
    }
    getLeafNodeCount(root.left);
    getLeafNodeCount(root.right);
        return count;
    }

    public int getLeafNodeCount1(TreeNode root){
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right == null){
            return 1;
        }
        int c = getLeafNodeCount1(root.left)+getLeafNodeCount1(root.right);

        return c;
    }
}

